import {
  Controller,
  Post,
  Body,
  Get,
  Param,
  Req,
  HttpException,
  HttpStatus,
} from '@nestjs/common';
import Redis from 'ioredis';
import { InjectRedis } from '@nestjs-modules/ioredis';
import { ApiTags } from '@nestjs/swagger';

import { CreateUserDto, LoginUserDto, UserDetailDto } from './dto/user.dto';
import { UserService } from './user.service';

import { JwtPass } from '@/core/decorator';
import { AnalyticsService } from '../analytics/analytics.service';
import { EventType } from '../entities';

@ApiTags('小程序/用户')
@Controller('app/user')
export class UserController {
  constructor(
    private readonly userService: UserService,
    private readonly analyticsService: AnalyticsService,
    @InjectRedis() private readonly redis: Redis,
  ) {}

  /**
   * 用户创建
   */
  @Post()
  @JwtPass()
  async createUser(@Body() user: CreateUserDto) {
    await this.userService.create(user);
  }

  /**
   * 用户登录（返回token）
   */
  @Post('login')
  @JwtPass()
  async loginUser(@Body() data: LoginUserDto) {
    const { token, user } = await this.userService.login(data);
    if (token) {
      this.analyticsService.create(user, EventType.VISIT);
    }
    return token;
  }

  /**
   * 我的信息
   */
  @Get()
  async curUserDetail(@Req() req: Request): Promise<UserDetailDto> {
    await this.analyticsService.create(req.user, EventType.PROFILE_VIEW);

    return this.userService.findCurUser(req.user);
  }

  // /**
  //  * 用户详情
  //  */
  // @Get(':userId')
  // async userDetail(userId: number): Promise<UserDetailDto> {
  //   return this.userService.findOne(userId);
  // }
}
